package com.igeek.javase.ch05.treemaps;

/*
 *@author wangpeng
 *2023/7/17 18:45
 *@description TODO
 */
/**
 * 功能需求：
 * 创建User类  属性name , age
 * 按照姓名的升序进行排列，姓名相同则按照年龄的降序进行排序，姓名和年龄都相同，则不进行存储
 */
import java.util.TreeMap;
import java.util.Comparator;
public class UserTest {
    public static void main(String[] args) {
        TreeMap<User, Integer> userMap = new TreeMap<>(new Comparator<User>() {
            @Override
            public int compare(User u1, User u2) {
                // 按照姓名升序
                int nameCompare = u1.getName().compareTo(u2.getName());
                if (nameCompare != 0) {
                    return nameCompare;
                }
                // 姓名相同，按照年龄降序
                return Integer.compare(u2.getAge(), u1.getAge());
            }
        });

        User user1 = new User("zhangyu", 25);
        User user2 = new User("tianjing", 30);
        User user3 = new User("zhangyu", 20);
        User user4 = new User("xutao", 35);
        User user5 = new User("shileipeng", 30);
        User user6 = new User("tianjing",30);

        userMap.put(user1, 1);
        userMap.put(user2, 2);
        userMap.put(user3, 3);
        userMap.put(user4, 4);
        userMap.put(user5, 5);
        userMap.put(user6, 6);

        for (User user : userMap.keySet()) {
            System.out.println(user);
        }
    }
}
